//
//  XXCenterViewController.m
//  XX
//
//  Created by lly on 15/5/25.
//  Copyright (c) 2015年 lly. All rights reserved.
//

#import "XXCenterViewController.h"
#import "AppMacro.h"
#import "XXToolView.h"
#import "XXCollectionViewCell.h"
#import "UIViewExt.h"
#import "XXADView.h"
#import "XXDrawerViewController.h"
#import "XXPositionViewController.h"
#import "XXCategoryViewController.h"
#import "XXDetailViewController.h"
#import "XXNaviShowView.h"
#import "XXLocationViewController.h"
#import "XXNavigationController.h"
#define HEAD_SIZE 190
#define AddressView_Tag  10000
#define CategoryView_Tag 20000
#define CloseView_Tag  30000
@interface XXCenterViewController () <UICollectionViewDelegateFlowLayout,UIScrollViewDelegate>
{
    NSString *_identify;
    CGFloat lastOffsetY;
    CGFloat originOffsetY;
    BOOL isDecelerating;

}
@property (strong, nonatomic) IBOutlet UICollectionView *collectView;
@property (strong, nonatomic) IBOutlet UICollectionViewFlowLayout *collectFlowLayout;
@property (strong, nonatomic) XXToolView *toolView;

@end

@implementation XXCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self defaultTilteView];
    [self defaultToolView];
    [self defaultCollectView];
    [self defaultNavigationButton];

    originOffsetY = _collectView.contentOffset.y;

//    self.collectView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);

}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    _collectView.frame = CGRectMake(0, 50, SCREEN_WIDTH, SCREEN_HEIGHT - 50);

}
- (void)defaultNavigationButton
{
    // 左
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 40, 30);
    backButton.backgroundColor = [UIColor clearColor];
    [backButton setBackgroundImage:[UIImage imageNamed:@"sliding_menu_up.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(goLeftSideSelector) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    //右
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0, 40, 30);
    rightButton.backgroundColor = [UIColor clearColor];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"sliding_menu_up.png"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(goLeftSideSelector) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];

}

- (void)defaultCollectView {
    //为当前UICollectionView对象创建布局对象
    _collectFlowLayout.itemSize = CGSizeMake(155, 180);
    _collectFlowLayout.minimumLineSpacing = 6;
    _collectFlowLayout.minimumInteritemSpacing = 6;
    _collectFlowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH / 320 * HEAD_SIZE);
    _collectFlowLayout.itemSize = CGSizeMake((SCREEN_WIDTH - 10)/2, (SCREEN_WIDTH - 10)/2 /155 *180 );


    _collectView.delegate = self;
    _collectView.backgroundColor = [UIColor clearColor];

//    _collectView
    //注册单元格
    _identify = @"showSellCell";
    
    [_collectView registerNib:[UINib nibWithNibName:@"XXCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:_identify];
    [_collectView registerNib:[UINib nibWithNibName:@"XXADView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"collectHeader"];
//    [_collectView registerClass:[XXADView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"collectHeader"];
}


- (void)defaultToolView {
    self.toolView = [XXToolView XibView];
    self.toolView.contentFrame = CGRectMake(0, 64.0, SCREEN_WIDTH, 50);

    [self.view addSubview:self.toolView];
    
    __weak typeof(self) weakself = self;
    
    [self.toolView setSelectBlock:^(NSInteger index) {
        if(!index) {
            [weakself addAddressView];
//            [weakself.navigationController pushViewController:vc animated:YES];
        }
        else {
            [weakself addCategoryView];

        }
    }];
}

- (void)defaultTilteView {
    UILabel *titleView=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
    titleView.textColor = [UIColor whiteColor];
    titleView.textAlignment = UITextAlignmentCenter;
    titleView.text = @"森林果园";

    [titleView setContentMode:UIViewContentModeScaleAspectFit];

    [self.navigationController.navigationBar addSubview:titleView];


}

#pragma mark AddressView Action UI
- (void)addAddressView
{
    XXPositionViewController *vc = [[XXPositionViewController alloc] init];
    [self addChildViewController:vc];
    vc.view.frame = CGRectMake(0, 114, SCREEN_WIDTH, SCREEN_HEIGHT - 50 - 64);
    vc.view.tag = AddressView_Tag;
    vc.view.alpha = 0;
    [UIView animateWithDuration:0.2 animations:^{
        vc.view.alpha = 1;
        [self.view addSubview:vc.view];

        self.navigationController.navigationBar.frame = CGRectMake(self.navigationController.navigationBar.left,self.navigationController.navigationBar.top,self.navigationController.navigationBar.width,self.navigationController.navigationBar.frame.size.height + 44);
        
        [self addCloseButton];


    } completion:^(BOOL finished) {

        
    }];
}

- (void)addCloseButton
{
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.rightBarButtonItem = nil;
    
    //添加地址显示
    
    XXNaviShowView *showView = [XXNaviShowView XibView];
    showView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 88);
    showView.tag = CloseView_Tag;
    [self.navigationController.navigationBar addSubview:showView];
    __weak typeof(self) weakself = self;

    [showView setOnClickAction:^{
        XXLocationViewController *location = [[XXLocationViewController alloc] init];
        XXNavigationController *navig = [[XXNavigationController alloc] initWithRootViewController:location];
        [weakself presentViewController:navig animated:YES completion:nil];
    }];
    [showView setOnClickCloseAction:^{
        [weakself closeAddrSelect];
    }];
    
    
}

- (void)closeAddrSelect
{
    
    [UIView animateWithDuration:0.2 animations:^{
        self.navigationController.navigationBar.frame = CGRectMake(self.navigationController.navigationBar.left,self.navigationController.navigationBar.top,self.navigationController.navigationBar.width, 44);
        
        [self defaultNavigationButton];
        UIView *view1 = [self.view viewWithTag:AddressView_Tag];
        view1.alpha = 0;
        UIView *view2 = [self.view viewWithTag:CategoryView_Tag];
        view2.alpha = 0;
        UIView *view3= [self.navigationController.navigationBar viewWithTag:CloseView_Tag];
        view3.alpha = 0;
   
    } completion:^(BOOL finished) {
        UIView *view1= [self.view viewWithTag:AddressView_Tag];
        [view1 removeFromSuperview];
        UIView *view2= [self.view viewWithTag:CategoryView_Tag];
        [view2 removeFromSuperview];
        UIView *view3= [self.navigationController.navigationBar viewWithTag:CloseView_Tag];
        [view3 removeFromSuperview];
  
    }];
}

#pragma mark Category Action UI

- (void)addCategoryView
{
    XXCategoryViewController *vc = [[XXCategoryViewController alloc] init];
    [self addChildViewController:vc];
    vc.view.frame = CGRectMake(0, 114, SCREEN_WIDTH, SCREEN_HEIGHT - 50 - 64);
    vc.view.tag = CategoryView_Tag;
    vc.view.alpha = 0;
    [UIView animateWithDuration:0.2 animations:^{
        vc.view.alpha = 1;
        [self.view addSubview:vc.view];
        [self addCategoryCloseButton];
        } completion:^(BOOL finished) {
    }];
}

- (void)addCategoryCloseButton
{
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 30, 30);
    backButton.backgroundColor = [UIColor clearColor];
    [backButton setBackgroundImage:[UIImage imageNamed:@"close.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(closeAddrSelect) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/**
 *  开关抽屉的方法
 */
- (void)goLeftSideSelector
{
    if(self.drawerViewController.openSide == MMDrawerSideLeft)
    {
        [self.drawerViewController closeDrawerAnimated:YES completion:nil];
    }
    else
    {
        [self.drawerViewController openDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
    }
}

#pragma mark -UICollectionView

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        XXADView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"collectHeader" forIndexPath:indexPath];
        
        return headerView;
    }
    return nil;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 30;
}

// 单元格代理
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    XXCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:_identify forIndexPath:indexPath];
    
//    cell.backgroundColor = [UIColor redColor];
    

    
    return cell;
}



// 单元格选择代理
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    XXDetailViewController *detail = [[XXDetailViewController alloc] init];
    [self.navigationController pushViewController:detail animated:YES];
}
#pragma mark 控制toolview 显示隐藏
#pragma mark
#pragma Navigation hide Scroll

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    isDecelerating = YES;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    isDecelerating = NO;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    if(lastOffsetY < scrollView.contentOffset.y){//moving up
        
        NSLog(@"%f ... %f",scrollView.contentOffset.y,originOffsetY);

        if(self.toolView.top  > 64 - self.toolView.height && scrollView.contentOffset.y > originOffsetY){//not yet hidden
            float newY = self.toolView.contentFrame.origin.y - (scrollView.contentOffset.y - lastOffsetY);
            if(newY < 64 - self.toolView.contentFrame.size.height)
                newY = 64 - self.toolView.contentFrame.size.height;
            
            
            self.toolView.contentFrame = CGRectMake(self.toolView.left
                                                                       ,newY,
                                                                       self.toolView.width,
                                                                       self.toolView.height);

        }
    }else

        if(self.toolView.top < 64 && scrollView.contentOffset.y > originOffsetY){
            float newY = self.toolView.top + (lastOffsetY - scrollView.contentOffset.y);
            if(newY > 64)
                newY = 64;
            self.toolView.contentFrame = CGRectMake(self.toolView.contentFrame.origin.x,
                                                                       newY,
                                                                       self.toolView.contentFrame.size.width,
                                                                       self.toolView.contentFrame.size.height);
        }
    
    lastOffsetY = scrollView.contentOffset.y;
}





@end
